package com.ucar.train.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ucar.train.dto.MenuDTO;
import com.ucar.train.dto.RoleDTO;
import com.ucar.train.service.MenuService;
import com.ucar.train.service.impl.MenuServiceImpl;
import com.ucar.train.utils.GsonUtils;
import com.ucar.train.vo.Menu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.List;

/**
 * @author zhoujinmu
 * @title MenuController
 * @projectName train-parent
 * @description TODO
 * @created 2019-07-24 15:16
 **/
@Slf4j
@Controller
@RequestMapping("/menu")
public class MenuController {

  @Autowired
  private MenuServiceImpl menuService;

  @RequestMapping("/showMenu")
  public ModelAndView getMenuList(@RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size, @RequestParam(name = "orderBy", required = true, defaultValue = "id asc") String orderBy, HttpServletRequest httpServletRequest){
    ModelAndView modelAndView = new ModelAndView();
    List<MenuDTO> menuList = menuService.getMenu(page,size,orderBy);
    HttpSession httpSession = httpServletRequest.getSession();
    httpSession.setAttribute("orderBy",orderBy);
    PageInfo pageInfo = new PageInfo(menuList);
    log.info(menuList.toString());
    modelAndView.addObject("pageInfo",pageInfo);
    modelAndView.setViewName("order-list");
    return modelAndView;
  }

  @RequestMapping("/updateMenu")
  public String updateMenu(Menu menu){
    menuService.updateMenu(menu);
    return "redirect:showMenu";
  }
  @RequestMapping("/addMenu")
  public String getRoleList(Menu menu){
    menuService.addMenu(menu);
    return "redirect:showMenu";
  }
  @RequestMapping("/deleteMenu")
  public String getRoleList(@RequestParam("id")Long id){

    menuService.deleteMenu(id);
    return "redirect:showMenu";
  }

  @RequestMapping("/getMenuById")
  public ModelAndView getMenuById(@RequestParam("id")Long id){
    ModelAndView modelAndView = new ModelAndView();
    MenuDTO menu = menuService.getMenuById(id);
    modelAndView.addObject("menu",menu);
    modelAndView.setViewName("menus-update");
    return modelAndView;
  }

}
